<?php
namespace Application\Api\Controller;

class Team extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_TEAM);
    }

    public function indexAction()
    {
        $data = $this->getRequestData();
        $send = $this->getService()->getIndex($data['page'], $data['limit']);
        $this->sendSuccess($send);
    }

    private function setStatus($data)
    {
        if(isset($data['dry_status']) && $data['dry_status'] == 'on'){
            $data['dry_status'] = 1;
        }
        else{
            $data['dry_status'] = 0;
        }
        return $data;
    }

    public function newAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $data = addDateTime($data,THE_DATE);
        $data['dry_user'] = $userId;
        $data['dry_data_sort'] = 0;
        $data['dry_sort'] = isset($data['dry_sort']) ? $data['dry_sort'] : 0;
        $data['dry_status'] = isset($data['dry_status']) ? $data['dry_status'] : 1;
        $id = $service->add($data);
        $send = [
            'id' => $id
        ];
        $this->sendSuccess($send);
    }

    public function editAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $rows = $service->update($data, $id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function deleteAction(){
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        $rows = $service->delete($id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function getListAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $temp = $this->getService()->getMoreByField('dry_user',$userId);
        $list = $this->get(SERVICE_OSSFILE)->getFileData($temp, ['dry_image' => 1]);
        $employee = $this->get(SERVICE_EMPLOYEE)->getMoreByField('dry_employee',$userId);
        if(!empty($employee)){
            $ids = [];
            foreach ($employee as $v){
                array_push($ids,$v->dry_team);
            }
            $teamIds = implode(',',$ids);
            $tempt = $this->getService()->getListByFieldsIn('id',$teamIds);
            $teams = $this->get(SERVICE_OSSFILE)->getFileData($tempt, ['dry_image' => 1]);
            foreach ($teams as $v){
                array_push($list,$v);
            }
        }
        $data = [
            'oss_domain' => getOssDomain().'/',
            'list' => $list,
        ];
        $this->sendSuccess($data);
    }

    public function getOneByIdAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $id = $data['id'];
        $temp = $this->getService()->one($id);
        $list = $this->get(SERVICE_OSSFILE)->getFileData([$temp], ['dry_image' => 1]);
        $data = [
            'oss_domain' => getOssDomain().'/',
            'list' => $list,
        ];
        $this->sendSuccess($data);
    }

    /*
        /Api/Team/getUploadData
    */
    public function getUploadDataAction()
    {
        $data = $this->get(SERVICE_OSS)->getUploadData('spwork');
        $this->sendSuccess($data);
    }

}